package constant.milk.cashbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;

import constant.milk.cashbook.asset.AssetActivity;
import constant.milk.cashbook.auto.AutoActivity;
import constant.milk.cashbook.bean.AptData;
import constant.milk.cashbook.bean.BankData;
import constant.milk.cashbook.bean.CheckData;
import constant.milk.cashbook.bean.CreditData;
import constant.milk.cashbook.bean.GiftData;
import constant.milk.cashbook.bean.InOutData;
import constant.milk.cashbook.bean.MainSettingData;
import constant.milk.cashbook.bean.MyHouseData;
import constant.milk.cashbook.bean.PhoneData;
import constant.milk.cashbook.bean.RealData;
import constant.milk.cashbook.bean.SmsData;
import constant.milk.cashbook.bean.SmsInputData;
import constant.milk.cashbook.budgetstats.BudgetStatsActivity;
import constant.milk.cashbook.category.CategoryActivity;
import constant.milk.cashbook.data.DataBankUpActivity;
import constant.milk.cashbook.data.DataMainActivity;
import constant.milk.cashbook.database.DBSelectQuery;
import constant.milk.cashbook.database.DbAdapter;
import constant.milk.cashbook.database.DbInsertQuery;
import constant.milk.cashbook.database.TableName;
import constant.milk.cashbook.house.HouseTitleInsertActvitiy;
import constant.milk.cashbook.insert.InsertActivity;
import constant.milk.cashbook.main.MainInsertActivity;
import constant.milk.cashbook.main.MainUpdateActivity;
import constant.milk.cashbook.select.SelectListActivity;
import constant.milk.cashbook.select.SelectMainActivity;
import constant.milk.cashbook.sms.SmsGet;
import constant.milk.cashbook.sms.parser.SmsParser;
import constant.milk.cashbook.tip.TipActivity;
import constant.milk.cashbook.util.CalendarActivity;
import constant.milk.cashbook.util.MilkManager;
import constant.milk.cashbook.util.MilkTextView;
import constant.milk.cashbook.util.MilkUtil;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.DrawerLayout.DrawerListener;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends SuperActivity
{
	public static final int REQUEST_CALENDER = 0;
	public static final int REQUEST_HOUSE = 1;
	
	public static final String MAIN_SELECT_FLAG = "MAIN_SELECT_FLAG";
	public static final String MAIN_SELECT_TYPE = "MAIN_SELECT_TYPE";
	public static final String MAIN_SELECT_ASSET = "MAIN_SELECT_ASSET";
	public static final String MAIN_SELECT_NAME = "MAIN_SELECT_NAME";
	public static final String MAIN_SELECT_FAN = "MAIN_SELECT_FAN";
	
	private View todayView;
	private View weekdayView;
	private View monthView;
	private View yearView;
	
	private View preView;
	private View nextView;
	private View houseView;
	
	private View dateView;
	private TextView dateTextView;
	
	private ListView listView;
	
	private View insertView;	
	private View selectView;	
	private View assetManagerView;	
	private View dataView;
	private View categoryView;
	private View autoView;
	private View budgetStatsView;
	private View tipView;
	
	private boolean backFlag;
	
	private int year;
	private int month;
	private int day;
	
	private int year1;
	private int month1;
	private int day1;
	
	private int year2;
	private int month2;
	private int day2;
	
	private int dateKind;
	
	private DBSelectQuery select;
	
	private MilkManager milkManager;
	
	private int startDate;
	
	private ArrayList<SmsData> smsList;
	
	private View houseDrawerLeftView;
	private TextView houseAreaTextView;
	private TextView houseMoneyTextView;
	private TextView houseMonthMoneyTextView;
	private TextView houseResultMoneyTextView;
	private TextView houseTimeTextView;
	private View houseSettingView;
	
	private ImageView houseImageView;
	private TextView houseMessageTextView;
	private TextView houseMeibJunseTextView;
	private TextView houseSizeTextView;
	private TextView housePercentTextView;
	private TextView houseDayTextView;
	
	private DrawerLayout drawer;
	
	private Handler backHandler = new Handler()
    {
		@Override
		public void handleMessage(Message msg)
		{
			super.handleMessage(msg);
			
			if (msg.what == 0)
			{
				backFlag = false;
			}
		}
    };
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.main_activity);
		
		setGoneTitle();
		
		backFlag = false;
		
		setFindViewId();
		setOnClick();
		setData();
	}
	
	@Override
	protected void onResume()
	{
		super.onResume();
		
		setSettingData();
	}

	@Override
	protected void setFindViewId()
	{
		todayView = findViewById(R.id.mainTodayView);
		weekdayView = findViewById(R.id.mainWeekdayView);
		monthView = findViewById(R.id.mainMonthView);
		yearView = findViewById(R.id.mainYearView);
		
		preView = findViewById(R.id.mainPreView);
		nextView = findViewById(R.id.mainNextView);
		houseView = findViewById(R.id.mainHouseLeftView);
		dateTextView = (TextView) findViewById(R.id.mainDateTextView);

		dateView = findViewById(R.id.mainDateView);
		
		listView = (ListView) findViewById(R.id.listView);
		
		insertView = findViewById(R.id.mainInsertView);
		selectView = findViewById(R.id.mainSelectView);
		assetManagerView = findViewById(R.id.mainAssetManagerView);
		dataView = findViewById(R.id.mainDataView);
		categoryView = findViewById(R.id.mainCategoryView);
		autoView = findViewById(R.id.mainAutoView);
		budgetStatsView = findViewById(R.id.mainBudgetStatsView);
		tipView = findViewById(R.id.mainTipView);
		
		houseDrawerLeftView = findViewById(R.id.mainDrawerLeftView);
		houseAreaTextView = (TextView) findViewById(R.id.mainHouseAreaTextView);
		houseMoneyTextView = (TextView) findViewById(R.id.mainHouseMoneyTextView);
		houseMonthMoneyTextView = (TextView) findViewById(R.id.mainHouseMonthMoneyTextView);
		houseResultMoneyTextView = (TextView) findViewById(R.id.mainHouseResultMoneyTextView);
		houseTimeTextView = (TextView) findViewById(R.id.mainHouseTimeTextView);
		houseSettingView = findViewById(R.id.mainHouseSettingView);;
		
		houseMessageTextView = (TextView) findViewById(R.id.mainHouseMessageTextView);
		houseImageView = (ImageView) findViewById(R.id.mainHouseImageView);
		houseMeibJunseTextView = (TextView) findViewById(R.id.mainHouseMeibJunseTextView);
		houseSizeTextView = (TextView) findViewById(R.id.mainHouseSizeTextView);
		housePercentTextView = (TextView) findViewById(R.id.mainHousePercentTextView);;
		houseDayTextView = (TextView) findViewById(R.id.mainHouseDayTextView);
		
		drawer = (DrawerLayout) findViewById(R.id.mainDrawer);
	}

	@Override
	protected void setOnClick()
	{
		todayView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				dateKind = 0;
				
				setSettingData();
			}
		});
		
		weekdayView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				dateKind = 1;
				
				setSettingData();
			}
		});
		
		monthView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				dateKind = 2;
				
				setSettingData();
			}
		});
		
		yearView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				dateKind = 3;
				
				setSettingData();
			}
		});
		
		preView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				preDate();
			}
		});
		
		nextView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				nextDate();
			}
		});
		
		dateView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, CalendarActivity.class);
				intent.putExtra(CalendarActivity.START_DATE_FLAG, 0);
				intent.putExtra(CalendarActivity.YEAR, year);
				intent.putExtra(CalendarActivity.MONTN, month);
				intent.putExtra(CalendarActivity.DAY, day);
				startActivityForResult(intent, REQUEST_CALENDER);
			}
		});
		
		insertView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, InsertActivity.class);
				startActivity(intent);
			}
		});
		
		selectView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, SelectMainActivity.class);
				startActivity(intent);
			}
		});
		
		assetManagerView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, AssetActivity.class);
				startActivity(intent);
			}
		});
		
		dataView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, DataMainActivity.class);
				startActivity(intent);
			}
		});
		
		categoryView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, CategoryActivity.class);
				startActivity(intent);
			}
		});
		
		autoView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, AutoActivity.class);
				startActivity(intent);
			}
		});
		
		budgetStatsView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, BudgetStatsActivity.class);
				startActivity(intent);
			}
		});
		
		tipView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, TipActivity.class);
				startActivity(intent);
			}
		});
		
		houseSettingView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, HouseTitleInsertActvitiy.class);
				intent.putExtra(HouseTitleInsertActvitiy.HOUSE_TITLE, "");
				startActivityForResult(intent, REQUEST_HOUSE);
			}
		});
		
		drawer.setDrawerListener(new DrawerListener()
		{
			@Override
			public void onDrawerStateChanged(int arg0)
			{
			}
			
			@Override
			public void onDrawerSlide(View arg0, float arg1)
			{
			}
			
			@Override
			public void onDrawerOpened(View arg0)
			{
				setDrawerData();
			}

			@Override
			public void onDrawerClosed(View arg0)
			{
			}
		});
		
		houseView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				drawer.openDrawer(houseDrawerLeftView);
			}
		});
	}

	@Override
	protected void setData()
	{
		SharedPreferences pref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
		
		select = new DBSelectQuery(superContext, superDb);
		milkManager = new MilkManager(superContext, superDb);
		
		Calendar cal = Calendar.getInstance();
		
		year = cal.get(Calendar.YEAR);
		month = cal.get(Calendar.MONTH);
		day = cal.get(Calendar.DAY_OF_MONTH);
		
		dateKind = pref.getInt(getString(R.string.prefMainDateKind), 2);
		
		View view = getLayoutInflater().inflate(R.layout.main_footer_view, null);
		
		listView.addFooterView(view, null, false);
		
		((MilkTextView) view.findViewById(R.id.mainFooterTextView)).setTextSize(TEXT_SIZE);
		view.findViewById(R.id.mainFooterView).setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, MainInsertActivity.class);
				startActivity(intent);
			}
		});
		
		setShowTipFristDialog();
	}
	
	private void preDate()
	{
		Calendar cal = Calendar.getInstance();
		cal.set(year, month, day);
		
		if (dateKind == 0)
		{
			cal.add(Calendar.DAY_OF_MONTH, -1);
		}
		else if (dateKind == 1)
		{
			cal.add(Calendar.DAY_OF_MONTH, -7);
		}
		else if (dateKind == 2)
		{
			cal.add(Calendar.MONTH, -1);
		}
		else if (dateKind == 3)
		{
			cal.add(Calendar.YEAR, -1);
		}
		
		year = cal.get(Calendar.YEAR);
		month = cal.get(Calendar.MONTH);
		day = cal.get(Calendar.DAY_OF_MONTH);
		
		setSettingData();
	}
	
	private void nextDate()
	{
		Calendar cal = Calendar.getInstance();
		cal.set(year, month, day);
		
		if (dateKind == 0)
		{
			cal.add(Calendar.DAY_OF_MONTH, 1);
		}
		else if (dateKind == 1)
		{
			cal.add(Calendar.DAY_OF_MONTH, 7);
		}
		else if (dateKind == 2)
		{
			cal.add(Calendar.MONTH, 1);
		}
		else if (dateKind == 3)
		{
			cal.add(Calendar.YEAR, 1);
		}
		
		year = cal.get(Calendar.YEAR);
		month = cal.get(Calendar.MONTH);
		day = cal.get(Calendar.DAY_OF_MONTH);
		
		setSettingData();
	}
	
	private void setSettingData()
	{
		SharedPreferences pref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
		startDate = pref.getInt(getString(R.string.prefStartDate), 1);
		
		SharedPreferences.Editor editor = pref.edit();
		editor.putInt(getString(R.string.prefMainDateKind), dateKind);
		editor.commit();
		
		if (dateKind == 0)
		{
			setToday();
		}
		else if (dateKind == 1)
		{
			setWeekday();
		}
		else if (dateKind == 2)
		{
			setMonth();
		}
		else if (dateKind == 3)
		{
			setYear();
		}
	}
	
	private void setToday()
	{
		int padding = MilkUtil.dipToPixel(superContext, 10);
		todayView.setBackgroundResource(R.drawable.btn_neon_push);
		todayView.setPadding(padding, padding, padding, padding);
		weekdayView.setBackgroundResource(0);
		weekdayView.setPadding(padding, padding, padding, padding);
		monthView.setBackgroundResource(0);
		monthView.setPadding(padding, padding, padding, padding);
		yearView.setBackgroundResource(0);
		yearView.setPadding(padding, padding, padding, padding);
		
		setDateToday();
		setMainData();
	}
	
	private void setWeekday()
	{
		int padding = MilkUtil.dipToPixel(superContext, 10);
		todayView.setBackgroundResource(0);
		todayView.setPadding(padding, padding, padding, padding);
		weekdayView.setBackgroundResource(R.drawable.btn_neon_push);
		weekdayView.setPadding(padding, padding, padding, padding);
		monthView.setBackgroundResource(0);
		monthView.setPadding(padding, padding, padding, padding);
		yearView.setBackgroundResource(0);
		yearView.setPadding(padding, padding, padding, padding);
		
		setDateWeekday();
		setMainData();
	}
	
	private void setMonth()
	{
		int padding = MilkUtil.dipToPixel(superContext, 10);
		todayView.setBackgroundResource(0);
		todayView.setPadding(padding, padding, padding, padding);
		weekdayView.setBackgroundResource(0);
		weekdayView.setPadding(padding, padding, padding, padding);
		monthView.setBackgroundResource(R.drawable.btn_neon_push);
		monthView.setPadding(padding, padding, padding, padding);
		yearView.setBackgroundResource(0);
		yearView.setPadding(padding, padding, padding, padding);
		
		setDateMonth();
		setMainData();
	}
	
	private void setYear()
	{
		int padding = MilkUtil.dipToPixel(superContext, 10);
		todayView.setBackgroundResource(0);
		todayView.setPadding(padding, padding, padding, padding);
		weekdayView.setBackgroundResource(0);
		weekdayView.setPadding(padding, padding, padding, padding);
		monthView.setBackgroundResource(0);
		monthView.setPadding(padding, padding, padding, padding);
		yearView.setBackgroundResource(R.drawable.btn_neon_push);
		yearView.setPadding(padding, padding, padding, padding);
		
		setDateYear();
		setMainData();
	}
	
	private void setDateToday()
	{
		Calendar cal = Calendar.getInstance();
		cal.set(year, month, day);
		
		year1 = cal.get(Calendar.YEAR);
		month1 = cal.get(Calendar.MONTH);
		day1 = cal.get(Calendar.DAY_OF_MONTH);
		
		year2 = cal.get(Calendar.YEAR);
		month2 = cal.get(Calendar.MONTH);
		day2 = cal.get(Calendar.DAY_OF_MONTH);
		
		dateTextView.setText(year + "년 " + (month+1) + "월 " + day + "일");
	}
	
	private void setDateWeekday()
	{
		Calendar cal1 = Calendar.getInstance();
		cal1.set(year, month, day);
		
		Calendar cal2 = Calendar.getInstance();
		cal2.set(year, month, day);
		
		Calendar cal3 = Calendar.getInstance();
		cal3.set(year, month, day);
		
		int weekday = cal2.get(Calendar.DAY_OF_WEEK);
		
		cal1.add(Calendar.DAY_OF_MONTH, -(weekday - 1));
		cal2.add(Calendar.DAY_OF_MONTH, (7 - weekday));
		
		year1 = cal1.get(Calendar.YEAR);
		month1 = cal1.get(Calendar.MONTH);
		day1 = cal1.get(Calendar.DAY_OF_MONTH);
		
		year2 = cal2.get(Calendar.YEAR);
		month2 = cal2.get(Calendar.MONTH);
		day2 = cal2.get(Calendar.DAY_OF_MONTH);
		
		dateTextView.setText(year + "년 " + (month+1) + "월 " + cal3.get(Calendar.WEEK_OF_MONTH) + "째주");
	}
	
	private void setDateMonth()
	{
		Calendar cal1 = Calendar.getInstance();
		cal1.set(year, month, day);
		
		Calendar cal2 = Calendar.getInstance();
		cal2.set(year, month, day);
		
		if (startDate == 1)
		{
			year1 = cal1.get(Calendar.YEAR);
			month1 = cal1.get(Calendar.MONTH);
			day1 = startDate;
			
			year2 = cal2.get(Calendar.YEAR);
			month2 = cal2.get(Calendar.MONTH);
			day2 = cal2.getActualMaximum(Calendar.DAY_OF_MONTH);
		}
		else if (day >= startDate)
		{
			year1 = cal1.get(Calendar.YEAR);
			month1 = cal1.get(Calendar.MONTH);
			day1 = startDate;
			
			cal2.add(Calendar.MONTH, 1);
			year2 = cal2.get(Calendar.YEAR);
			month2 = cal2.get(Calendar.MONTH);
			day2 = startDate - 1;
		}
		else
		{
			cal1.add(Calendar.MONTH, -1);
			year1 = cal1.get(Calendar.YEAR);
			month1 = cal1.get(Calendar.MONTH);
			day1 = startDate;
			
			year2 = cal2.get(Calendar.YEAR);
			month2 = cal2.get(Calendar.MONTH);
			day2 = startDate - 1;
		}
		
		dateTextView.setText(year1 + "년 " + (month1+1) + "월 " + "(시작일 : " + startDate + ")");
	}
	
	private void setDateYear()
	{
		Calendar cal = Calendar.getInstance();
		cal.set(year, month, day);
		
		year1 = cal.get(Calendar.YEAR);
		month1 = cal.getActualMinimum(Calendar.MONTH);
		day1 = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
		
		year2 = cal.get(Calendar.YEAR);
		month2 = cal.getActualMaximum(Calendar.MONTH);
		day2 = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		
		dateTextView.setText(year + "년");
	}
	
	private void setMainData()
	{
		ArrayList<MainSettingData> list = select.getMainSettingList();
		
		listView.setOnItemClickListener(null);
		
		MainListAdapter adapter = new MainListAdapter(
				superContext, R.layout.main_list_item, list);
		listView.setAdapter(adapter);
	}
	
	@Override
	public void onBackPressed()
	{
		if (!backFlag)
		{
			MilkUtil.showToast(superContext, "뒤로 버튼을 한번더 누르시면 종료됩니다.");
			backFlag = true;
			
			backHandler.sendEmptyMessageDelayed(0, 2000);
		}
    	else
    	{
    		setAutoData();
    		superDb = DbAdapter.getInstance();
    		superDb.close();
    		super.onBackPressed();
    	}
	}
	
	/**
	 * 메인 리스트 아답터
	 */
	private class MainListAdapter extends ArrayAdapter<MainSettingData> 
	{
		public MainListAdapter(Context context, int textViewResourceId, ArrayList<MainSettingData> list) 
		{
			super(context, textViewResourceId, list);
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) 
		{
			View v = convertView;
			
			if (v == null) 
			{
				LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = inflater.inflate(R.layout.main_list_item, null);
			}
			
			MainSettingData data = getItem(position);
			
			ImageView iv = (ImageView) v.findViewById(R.id.mainItemIconImageView);
			MilkTextView tv1 = (MilkTextView) v.findViewById(R.id.mainItemNameTextView);
			MilkTextView tv2 = (MilkTextView) v.findViewById(R.id.mainItemMoneyTextView);
			View iconView = v.findViewById(R.id.mainItemIconView);
			
			iconView.setTag(data.getId());
			tv1.setTextSize(TEXT_SIZE);
			tv1.setTag(data.getId());
			tv2.setTextSize(TEXT_SIZE);
			tv2.setTag(data);
			
			if (data.getType().equals("0"))
			{
				tv2.setTextColor(getResources().getColor(R.color.outColor));
			}
			else if (data.getType().equals("1"))
			{
				tv2.setTextColor(getResources().getColor(R.color.inColor));
			}
			else if (data.getType().equals("2"))
			{
				tv2.setTextColor(getResources().getColor(R.color.moveColor));
			}
			else if (data.getType().equals("3"))
			{
				tv2.setTextColor(getResources().getColor(R.color.fanColor));
			}
			else if (data.getType().equals("4"))
			{
				tv2.setTextColor(getResources().getColor(R.color.balanceColor));
			}
			
			iv.setImageResource(getImage(data.getType()));
			
			if (data.getType().equals("3"))
			{
				tv1.setText(milkManager.getMainFanId(data.getKind()));
			}
			else
			{
				if (data.getAsset().equals("6"))
				{
					tv1.setText(getString(R.string.allAsset));
				}
				else 
				{
					if (data.getName().equals(""))
					{
						tv1.setText(getAssetAll(data.getAsset()));
					}
					else
					{
						tv1.setText(data.getName());
					}
				}
			}
			
			tv2.setText(MilkUtil.getWon(getMoney(data)));
			
			iconView.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					Intent intent = new Intent(superContext, MainUpdateActivity.class);
					intent.putExtra(MainUpdateActivity.MAIN_SETTING_ID, v.getTag().toString());
					startActivity(intent);
				}
			});
			
			tv2.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					MainSettingData data = (MainSettingData) v.getTag();
					
					if (data.getType().equals("4"))
					{
						MilkUtil.showToast(superContext, "잔액은 내역을 조회 하실 수 없습니다.");
						return;
					}
					
					Intent intent = new Intent(superContext, SelectListActivity.class);
					intent.putExtra(SelectMainActivity.YEAR1_DATA, year1);
					intent.putExtra(SelectMainActivity.MONTH1_DATA, month1);
					intent.putExtra(SelectMainActivity.DAY1_DATA, day1);
					intent.putExtra(SelectMainActivity.YEAR2_DATA, year2);
					intent.putExtra(SelectMainActivity.MONTH2_DATA, month2);
					intent.putExtra(SelectMainActivity.DAY2_DATA, day2);
					intent.putExtra(MAIN_SELECT_FLAG, 1);
					intent.putExtra(MAIN_SELECT_TYPE, data.getType());
					intent.putExtra(MAIN_SELECT_ASSET, data.getAsset());
					intent.putExtra(MAIN_SELECT_NAME, data.getName());
					intent.putExtra(MAIN_SELECT_FAN, data.getKind());
					
					startActivity(intent);
				}
			});
			
			return v;
		}
		
		private String getAssetAll(String asset)
		{
			if (asset.equals("0"))
			{
				return getString(R.string.allRealMoney);
			}
			else if (asset.equals("1"))
			{
				return getString(R.string.allBankbook);
			}
			else if (asset.equals("2"))
			{
				return getString(R.string.allCheckCard);
			}
			else if (asset.equals("3"))
			{
				return getString(R.string.allCreditCard);
			}
			else if (asset.equals("4"))
			{
				return getString(R.string.allPhone);
			}
			else if (asset.equals("5"))
			{
				return getString(R.string.allGiftCard);
			}
			
			return "";
		}

		private String getMoney(MainSettingData data)
		{
			if (data.getType().equals("4"))
			{
				if (data.getAsset().equals("0"))
				{
					if (data.getName().equals(""))
					{
						ArrayList<RealData> list = select.getRealList();
						
						BigDecimal money = BigDecimal.ZERO;
						
						for (int i = 0; i < list.size(); i++)
						{
							money = money.add(new BigDecimal(list.get(i).getMoney()));
						}
						
						return money.toString();
					}
					else
					{
						RealData assetData = select.getRealName(data.getName());
						
						return assetData.getMoney();
					}
				}
				else if (data.getAsset().equals("1"))
				{
					if (data.getName().equals(""))
					{
						ArrayList<BankData> list = select.getBankList();
						
						BigDecimal money = BigDecimal.ZERO;
						
						for (int i = 0; i < list.size(); i++)
						{
							money = money.add(new BigDecimal(list.get(i).getMoney()));
						}
						
						return money.toString();
					}
					else
					{
						BankData assetData = select.getBankName(data.getName());
						
						return assetData.getMoney();
					}
				}
				else if (data.getAsset().equals("2"))
				{
					if (data.getName().equals(""))
					{
						ArrayList<CheckData> list = select.getCheckList();
						
						BigDecimal money = BigDecimal.ZERO;
						
						for (int i = 0; i < list.size(); i++)
						{
							BankData bankData = select.getBankId(list.get(i).getLinkid());
							
							money = money.add(new BigDecimal(bankData.getMoney()));
						}
						
						return money.toString();
					}
					else
					{
						CheckData assetData = select.getCheckName(data.getName());
						BankData bankData = select.getBankId(assetData.getLinkid());
						
						return bankData.getMoney();
					}
				}
				else if (data.getAsset().equals("3"))
				{
					if (data.getName().equals(""))
					{
						ArrayList<CreditData> list = select.getCreditList();
						
						BigDecimal money = BigDecimal.ZERO;
						
						for (int i = 0; i < list.size(); i++)
						{
							money = money.add(new BigDecimal(list.get(i).getMoney()));
						}
						
						return money.toString();
					}
					else
					{
						CreditData assetData = select.getCreditName(data.getName());
						
						return assetData.getMoney();
					}
				}
				else if (data.getAsset().equals("4"))
				{
					if (data.getName().equals(""))
					{
						ArrayList<PhoneData> list = select.getPhoneList();
						
						BigDecimal money = BigDecimal.ZERO;
						
						for (int i = 0; i < list.size(); i++)
						{
							money = money.add(new BigDecimal(list.get(i).getMoney()));
						}
						
						return money.toString();
					}
					else
					{
						PhoneData assetData = select.getPhoneName(data.getName());
						
						return assetData.getMoney();
					}
				}
				else if (data.getAsset().equals("5"))
				{
					if (data.getName().equals(""))
					{
						ArrayList<GiftData> list = select.getGiftList();
						
						BigDecimal money = BigDecimal.ZERO;
						
						for (int i = 0; i < list.size(); i++)
						{
							BigDecimal tempMoney = new BigDecimal(list.get(i).getMoney());
							
							tempMoney = tempMoney.multiply(new BigDecimal(list.get(i).getNumber()));
							
							money = money.add(tempMoney);
						}
						
						return money.toString();
					}
					else
					{
						GiftData assetData = select.getGiftName(data.getName());
						
						BigDecimal money = new BigDecimal(assetData.getMoney());
						
						money = money.multiply(new BigDecimal(assetData.getNumber()));
						
						return money.toString();
					}
				}
				else if (data.getAsset().equals("6"))
				{
					ArrayList<RealData> realList = select.getRealList();
					ArrayList<BankData> bankList = select.getBankList();
					ArrayList<GiftData> giftList = select.getGiftList();
					
					BigDecimal money = BigDecimal.ZERO;
					
					for (int i = 0; i < realList.size(); i++)
					{
						money = money.add(new BigDecimal(realList.get(i).getMoney()));
					}
					
					for (int i = 0; i < bankList.size(); i++)
					{
						money = money.add(new BigDecimal(bankList.get(i).getMoney()));
					}
					
					for (int i = 0; i < giftList.size(); i++)
					{
						BigDecimal giftMoney = new BigDecimal(giftList.get(i).getMoney());
						
						giftMoney = giftMoney.multiply(new BigDecimal(giftList.get(i).getNumber()));
						
						money = money.add(giftMoney);
					}
					
					return money.toString();
				}
			}
			else
			{
				return getSumMoney(data);
			}
			
			return "0";
		}

		private String getSumMoney(MainSettingData data)
		{
			ArrayList<InOutData> list = new ArrayList<InOutData>();
			
			if (data.getType().equals("3"))
			{
				list = select.getInOutMainFanList(Integer.parseInt(data.getKind()), 
						MilkUtil.getDateDB(year1, month1+1, day1), MilkUtil.getDateDB(year2, month2+1, day2));
			}
			else if (data.getAsset().equals("3") && dateKind == 1)
			{
				if (data.getName().equals(""))
				{
					ArrayList<CreditData> creditList = select.getCreditList();
					
					for (int i = 0; i < creditList.size(); i++)
					{
						int termDate = Integer.parseInt(creditList.get(i).getTermdate());
						
						ArrayList<InOutData> inOutList = getTermDateInoutData(data, termDate);
						
						list.addAll(inOutList);
					}
				}
				else
				{
					CreditData creditData = select.getCreditName(data.getName());
					
					int termDate = Integer.parseInt(creditData.getTermdate());
					
					list = getTermDateInoutData(data, termDate);
				}
			}
			else if (data.getAsset().equals("4") && dateKind == 1)
			{
				if (data.getName().equals(""))
				{
					ArrayList<PhoneData> phoneList = select.getPhoneList();
					
					for (int i = 0; i < phoneList.size(); i++)
					{
						int termDate = Integer.parseInt(phoneList.get(i).getTermdate());
						
						ArrayList<InOutData> inOutList = getTermDateInoutData(data, termDate);
						
						list.addAll(inOutList);
					}
				}
				else
				{
					PhoneData phoneData = select.getPhoneName(data.getName());
					
					int termDate = Integer.parseInt(phoneData.getTermdate());
					
					list = getTermDateInoutData(data, termDate);
				}
			}
			else
			{
				list  = select.getInOutMainList(
						Integer.parseInt(data.getType()), Integer.parseInt(data.getAsset()), data.getName(), 
						MilkUtil.getDateDB(year1, month1+1, day1), MilkUtil.getDateDB(year2, month2+1, day2));
			}
			
			if (list == null || list.size() == 0)
			{
				return "0";
			}
			
			if (data.getAsset().equals("5"))
			{
				return milkManager.getSumOutGiftCardMoney(list);
			}
			else
			{
				return milkManager.getSumInOutMoney(list, data.getAsset());
			}
		}

		private int getImage(String type)
		{
			if (type.equals("0"))
			{
				return R.drawable.ic_mainout;
			}
			else if (type.equals("1"))
			{
				return R.drawable.ic_mainin;
			}
			else if (type.equals("2"))
			{
				return R.drawable.ic_mainmove;
			}
			else if (type.equals("3"))
			{
				return R.drawable.ic_fan;
			}
			else if (type.equals("4"))
			{
				return R.drawable.ic_mainbalance;
			}
			
			return R.drawable.ic_home;
		}
		
		private ArrayList<InOutData> getTermDateInoutData(MainSettingData data, int termDate)
		{
			Calendar cal1 = Calendar.getInstance();
			cal1.set(year, month, day);
			
			Calendar cal2 = Calendar.getInstance();
			cal2.set(year, month, day);
			
			int year1;
			int month1;
			int day1;
			
			int year2;
			int month2;
			int day2;
			
			if (termDate == 1)
			{
				year1 = cal1.get(Calendar.YEAR);
				month1 = cal1.get(Calendar.MONTH);
				day1 = 1;
				
				year2 = cal2.get(Calendar.YEAR);
				month2 = cal2.get(Calendar.MONTH);
				day2 = cal2.getActualMaximum(Calendar.DAY_OF_MONTH);
			}
			else if (day >= termDate)
			{
				year1 = cal1.get(Calendar.YEAR);
				month1 = cal1.get(Calendar.MONTH);
				day1 = termDate;
				
				cal2.add(Calendar.MONTH, 1);
				
				year2 = cal2.get(Calendar.YEAR);
				month2 = cal2.get(Calendar.MONTH);
				day2 = termDate - 1;
			}
			else
			{
				cal1.add(Calendar.MONTH, -1);
				
				year1 = cal1.get(Calendar.YEAR);
				month1 = cal1.get(Calendar.MONTH);
				day1 = termDate;
				
				year2 = cal2.get(Calendar.YEAR);
				month2 = cal2.get(Calendar.MONTH);
				day2 = termDate - 1;
			}
			
			return select.getInOutMainList(
						Integer.parseInt(data.getType()), Integer.parseInt(data.getAsset()), data.getName(), 
						MilkUtil.getDateDB(year1, month1+1, day1), MilkUtil.getDateDB(year2, month2+1, day2));
		}
	}
	
	/**
	 * 데이터를 자동으로 백업 하는 부분입니다.
	 */
	private void setAutoData()
	{
		try
		{
			if (!isDataCheck())
			{
				return;
			}
			
			File directory = new File(Environment.getExternalStorageDirectory() + 
					"/" + DataMainActivity.dirNameAuto);
			
			if (!directory.exists())
			{
				directory.mkdir();
			}
			
			File [] files = directory.listFiles(new FilenameFilter()
			{
				@Override
				public boolean accept(File dir, String filename)
				{
					return filename.endsWith(".db");
				}
			});
			
			if (files != null)
			{
				int location = 0;
				long date = Long.MAX_VALUE; 
				
				if (files.length >= DataBankUpActivity.AUTO_BACKUP_DATA_COUNT)
				{
					for (int i = 0; i < files.length; i++)
					{
						long tempDate = files[i].lastModified();
						
						if (tempDate < date)
						{
							location = i;
							date = tempDate;
						}
					}
					
					files[location].delete();
				}
			}
			
			setDataBackUp();
			
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	/**
	 * 디비에 데이터가 있는지 확인한다.
	 * @return
	 */
	private boolean isDataCheck()
	{
		int count = 0;
		
		count += select.getDBTableDataCount(TableName.IN_OUT);
		count += select.getDBTableDataCount(TableName.AUTO_MAIN);
		count += select.getDBTableDataCount(TableName.SMS);
		count += select.getDBTableDataCount(TableName.HALBU);
		count += select.getDBTableDataCount(TableName.BUDGET);
		
		if (count == 0)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	
	private int setDataBackUp()
	{
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH);
		int day = cal.get(Calendar.DAY_OF_MONTH);
		
		DbAdapter adapter = DbAdapter.getInstance();
		String path = adapter.getDbPath(superContext);
		adapter.close();
		
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try
		{
			File readfile = new File(path);
			
			if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
			{
				return 1;
			}
			
			fis = new FileInputStream(readfile);
			
			File dir = new File(
					Environment.getExternalStorageDirectory() + "/"
							+ DataMainActivity.dirName);
			
			if (!dir.exists())
			{
				dir.mkdir();
			}
			
			File directory = new File(Environment.getExternalStorageDirectory() + 
					"/" + DataMainActivity.dirNameAuto);
			
			if (!directory.exists())
			{
				directory.mkdir();
			}
			
			File writefile = new File(directory + "/" + DataBankUpActivity.FILE_NAME + "_AUTO_" 
					+ MilkUtil.getDateFile(year, month+1, day) + ".db");
			
			fos = new FileOutputStream(writefile);
			
			byte [] buffer = new byte[1024];
			
			int bufBytes = 0;
			while ((bufBytes = fis.read(buffer)) != -1) 
			{
				fos.write(buffer, 0, bufBytes);
			}
			fos.flush();
			
		} catch (Exception e)
		{
			return 2;
		} finally {
			try
			{
				if (fis != null) fis.close();
				if (fos != null) fos.close();
			} catch (IOException e)
			{
				e.printStackTrace();
				return 2;
			}
		}
		
		return 0;
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		super.onActivityResult(requestCode, resultCode, data);
		
		if (resultCode != Activity.RESULT_OK)
		{
			return;
		}
		
		if (requestCode == REQUEST_CALENDER)
		{
			Calendar cal = Calendar.getInstance();
			year = data.getIntExtra(CalendarActivity.YEAR, cal.get(Calendar.YEAR));
			month = data.getIntExtra(CalendarActivity.MONTN, cal.get(Calendar.MONTH));
			day = data.getIntExtra(CalendarActivity.DAY, cal.get(Calendar.DAY_OF_MONTH));
			
			setSettingData();
		}
		else if (requestCode == REQUEST_HOUSE)
		{
			setDrawerData();
		}
	}
	
	/**
	 * 다이얼로그 뛰우기 
	 */
	private class ShowDialog extends AsyncTask<Integer, Integer, Integer>
	{
		private int smsCount;
		
		ProgressDialog dialog = null;
		@Override
		protected void onPreExecute() 
		{
			super.onPreExecute();
			
			smsCount = 0;
			
			dialog = new ProgressDialog(MainActivity.this);
			dialog.setCancelable(false);
			dialog.setMessage("최근 3달의 문자내용을 가져와서 자동화 등록중 입니다. 잠시만 기다려주세요.");
			dialog.show();
		}
		
		@Override
		protected Integer doInBackground(Integer... arg0) 
		{
			if (arg0[0] == 0) 
			{
				Calendar cal1 = Calendar.getInstance();
				
				Calendar cal2 = Calendar.getInstance();
				cal2.add(Calendar.MONTH, -3);
				
				SmsGet smsGet = new SmsGet(superContext);
				smsList = smsGet.getSms(cal1.getTimeInMillis(), cal2.getTimeInMillis());
				
				for (int i = 0; i < smsList.size(); i++)
				{
					setSmsSaveData(smsList.get(i));
				}
				
				SharedPreferences pref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
				SharedPreferences.Editor editor = pref.edit();
				
				editor.putInt(getString(R.string.prefSmsAutoFirst), 1);
				editor.commit();
				
				return 0;
			}
			
			return 0;
		}

		@Override
		protected void onPostExecute(Integer result) 
		{
			super.onPostExecute(result);

			if (dialog != null) 
			{
				dialog.dismiss();
			}
			
			if (result == 0)
			{
				String text = "";
				
				if (smsList.size() == 0)
				{
					text = Html.fromHtml(getString(R.string.fristSmsNo)).toString();
				}
				else if (smsCount == 0)
				{
					text = Html.fromHtml(getString(R.string.fristSmsAssay)).toString();
				}
				else
				{
					text = "문자자동화가 " + smsCount + Html.fromHtml(getString(R.string.fristSmsOk)).toString();
				}
				
				if (text.equals(""))
				{
					return;
				}
				
				MilkUtil.showDialogOK(superContext, "알림", text, 
								new DialogInterface.OnClickListener()
								{
									@Override
									public void onClick(DialogInterface dialog, int which)
									{
//										setShowHouseFristDialog();
										
										dialog.dismiss();
									}
								});
			}
		}

		private boolean setSmsSaveData(SmsData data)
		{
			DbInsertQuery query = null;
			
			try
			{
				query = new DbInsertQuery(superContext, superDb);
				
				query.setBeginTansaction();
				
				SmsParser parser = new SmsParser(data.getMemo());
				
				SmsInputData smsData = parser.messageParsing();
				
				if (smsData == null || parser.getAvailable() == false)
				{
					return false;
				}
				
				if (parser.getSmsType() == SmsParser.HALBU_SMS)
				{
					return false;
				}
				
				ArrayList<SmsData> saveList = select.getSmsList();
				
				for (int i = 0; i < saveList.size(); i++)
				{
					SmsData saveData = saveList.get(i);
					
					if (saveData.getName().equals(smsData.getName()))
					{
						return false;
					}
				}
				
				HashMap<String, String> map = new HashMap<String, String>();
				
				map.put("address", data.getAddress());
				map.put("name", smsData.getName());
				map.put("memo", smsData.getMemo1());
				
				if (parser.getSmsType() == SmsParser.OUT_ATM_SMS)
				{
					return false;
				}
				else if (parser.getSmsType() == SmsParser.IN_ATM_SMS)
				{
					return false;
				}
				else if (parser.getSmsType() == SmsParser.OUT_SMS)
				{
					ArrayList<CreditData> list = select.getCreditList();
					
					map.put("cardname", list.get(0).getName());
					map.put("asset", milkManager.getAssetStr(getString(R.string.creditCardAsset)));
					map.put("type", SmsParser.OUT_SMS + "");
				}
				else if (parser.getSmsType() == SmsParser.IN_SMS)
				{
					ArrayList<BankData> list = select.getBankList();
					
					map.put("cardname", list.get(0).getName());
					map.put("asset", milkManager.getAssetStr(getString(R.string.bankAsset)));
					map.put("type", SmsParser.IN_SMS + "");
				}
				
				query.setSms(map);
				
				smsCount++;
				
				query.setSuccessful();
			} catch (Exception e)
			{
				e.printStackTrace();
				return false;
			} finally {
				try
				{
					query.setEndTansaction();
				} catch (Exception e)
				{
					e.printStackTrace();
					return false;
				}
			}
			
			return true;
		}
	}
	
	private void setShowTipFristDialog()
	{
		SharedPreferences pref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
		int tipFlag = pref.getInt(getString(R.string.prefTipFirst), 0);
		
		int version;

		try {
			PackageInfo i = getPackageManager().getPackageInfo(getPackageName(), 0);
			version = i.versionCode;
		} catch(NameNotFoundException e) { 
			version = 0;
		}
		
		if (tipFlag < version)
		{
			AlertDialog.Builder builder = new AlertDialog.Builder(superContext);
			builder.setTitle("공지");
			builder.setMessage(Html.fromHtml(getString(R.string.fristTip)).toString());
			builder.setIcon(android.R.drawable.ic_dialog_info);
			builder.setPositiveButton("확인", new DialogInterface.OnClickListener()
			{
				@Override
				public void onClick(DialogInterface dialog, int which)
				{
					SharedPreferences pref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
					int smsFlag = pref.getInt(getString(R.string.prefSmsAutoFirst), 0);
					
					if (smsFlag == 0)
					{
						new ShowDialog().execute(0);
					}
//					else
//					{
//						setShowHouseFristDialog();
//					}
					
					dialog.dismiss();
				}
			});
			
			builder.setNegativeButton("다시보지않기", new DialogInterface.OnClickListener()
			{
				@Override
				public void onClick(DialogInterface dialog, int which)
				{
					int version;

					try {
						PackageInfo i = getPackageManager().getPackageInfo(getPackageName(), 0);
						version = i.versionCode;
					} catch(NameNotFoundException e) { 
						version = 0;
					}
					
					SharedPreferences pref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
					SharedPreferences.Editor editor = pref.edit();
					editor.putInt(getString(R.string.prefTipFirst), version);
					editor.commit();
					
					int smsFlag = pref.getInt(getString(R.string.prefSmsAutoFirst), 0);
					
					if (smsFlag == 0)
					{
						new ShowDialog().execute(0);
					}
//					else
//					{
//						setShowHouseFristDialog();
//					}
					
					dialog.dismiss();
				}
			});
			
			AlertDialog dialog = builder.create();
			dialog.show();
		}
//		else
//		{
//			setShowHouseFristDialog();
//		}
	}
	
//	private void setShowHouseFristDialog()
//	{
//		SharedPreferences pref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
//		int houseFlag = pref.getInt(getString(R.string.prefHouseFirstFlag), 0);
//		
//		if (houseFlag == 0)
//		{
//			AlertDialog.Builder builder = new AlertDialog.Builder(superContext);
//			builder.setTitle("알림");
//			builder.setMessage(Html.fromHtml(getString(R.string.firstHouse)).toString());
//			builder.setIcon(android.R.drawable.ic_dialog_info);
//			builder.setPositiveButton("지금하기", new DialogInterface.OnClickListener()
//			{
//				@Override
//				public void onClick(DialogInterface dialog, int which)
//				{
//					SharedPreferences pref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
//					SharedPreferences.Editor editor = pref.edit();
//					editor.putInt(getString(R.string.prefHouseFirstFlag), 1);
//					editor.commit();
//					
//					Intent intent = new Intent(superContext, HouseTitleInsertActvitiy.class);
//					intent.putExtra(HouseTitleInsertActvitiy.HOUSE_TITLE, "");
//					startActivityForResult(intent, REQUEST_HOUSE);
//					
//					dialog.dismiss();
//				}
//			});
//			
//			builder.setNegativeButton("나중에하기", new DialogInterface.OnClickListener()
//			{
//				@Override
//				public void onClick(DialogInterface dialog, int which)
//				{
//					SharedPreferences pref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
//					SharedPreferences.Editor editor = pref.edit();
//					editor.putInt(getString(R.string.prefHouseFirstFlag), 1);
//					editor.commit();
//					
//					dialog.dismiss();
//				}
//			});
//			
//			AlertDialog dialog = builder.create();
//			dialog.show();
//		}
//	}
	
	private void setDrawerData()
	{
		MyHouseData myData = select.getMyHouse();
		
		if (myData == null)
		{
			houseMessageTextView.setText("설정된 집이 없습니다. 내 집 설정을 해주세요. ^^");
			return;
		}
		
		// 지역
		houseAreaTextView.setText(myData.getTitle() + " " + myData.getName());
		
		AptData aptData = select.getHouseTitleName(myData.getTitle(), myData.getName());
		
		// 목표 금액
		BigDecimal money = null;
		
		if (myData.getFlag().equals("0"))
		{
			money = new BigDecimal(aptData.getMeib() + "0000");
		}
		else
		{
			money = new BigDecimal(aptData.getJunse() + "0000");
		}
		
		BigDecimal pungMoney = new BigDecimal(money.multiply(new BigDecimal(3.3)).intValue());
		
		BigDecimal totalMoney = new BigDecimal(pungMoney.multiply(new BigDecimal(myData.getPung())).intValue());
		
		houseMoneyTextView.setText(MilkUtil.getWon(totalMoney.toString()));
		
		// 한달 저축금액
		BigDecimal inMoney = BigDecimal.ZERO;
		BigDecimal outMoney = BigDecimal.ZERO;
		
		int [] dates = MilkUtil.getDateSet(myData.getDate());
		
		Calendar cal = Calendar.getInstance();
		
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH);
		int day = cal.get(Calendar.DAY_OF_MONTH);
		
		ArrayList<InOutData> inList = select.getInOutList(1, 
				milkManager.getAssetStr(getString(R.string.all)), 
				"", "", "", "",
				MilkUtil.getDateDB(dates[0], dates[1], dates[2]),
				MilkUtil.getDateDB(year, month+1, day));
		
		ArrayList<InOutData> outList = select.getInOutList(0, 
				milkManager.getAssetStr(getString(R.string.all)), 
				"", "", "", "",
				MilkUtil.getDateDB(dates[0], dates[1], dates[2]),
				MilkUtil.getDateDB(year, month+1, day));
		
		for (int i = 0; i < inList.size(); i++)
		{
			InOutData data = inList.get(i);
			
			if (data.getType().equals("8") || data.getType().equals("3"))
			{
				continue;
			}
			
			inMoney = milkManager.getSumInOutMoney(data, inMoney);
		}
		
		for (int i = 0; i < outList.size(); i++)
		{
			InOutData data = outList.get(i);
			
			if (data.getType().equals("8") || data.getType().equals("3"))
			{
				continue;
			}
			
			outMoney = milkManager.getSumInOutMoney(data, outMoney);
		}
		
		BigDecimal firstDate = new BigDecimal(MilkUtil.getDateDB2(dates[0], dates[1]));
		BigDecimal lastDate = new BigDecimal(MilkUtil.getDateDB2(year, month+1));
		
		BigDecimal count = lastDate.subtract(firstDate);
		
		String monthMoney = "0";
		
		if (count.compareTo(BigDecimal.ZERO) > 0)
		{
			monthMoney = inMoney.subtract(outMoney).divide(count, 1, BigDecimal.ROUND_DOWN).toString();
			
			houseMonthMoneyTextView.setText(MilkUtil.getWon(monthMoney));
		}
		else
		{
			houseMonthMoneyTextView.setText("정보없음");
		}
		
		
		// 모은 금액
		BigDecimal bankMoney = BigDecimal.ZERO;
		ArrayList<BankData> bankList = select.getBankList();
		
		for (int i = 0; i < bankList.size(); i++)
		{
			BigDecimal tempMoney = new BigDecimal(bankList.get(i).getMoney());
			
			if (tempMoney.compareTo(BigDecimal.ZERO) > -1)
			{
				bankMoney = bankMoney.add(tempMoney);
			}
		}
		
		houseResultMoneyTextView.setText(MilkUtil.getWon(bankMoney.toString()));
		
		// 남은기간
		if (monthMoney.equals("0"))
		{
			houseTimeTextView.setText("평생");
		}
		else
		{
			String resultCount = 
					totalMoney.subtract(bankMoney).divide(new BigDecimal(monthMoney), 0, BigDecimal.ROUND_UP).toString();
			houseTimeTextView.setText(resultCount + " 개월");
		}
		
		// 매입, 전세
		if (myData.getFlag().equals("0"))
		{
			houseMeibJunseTextView.setText("매입");
		}
		else
		{
			houseMeibJunseTextView.setText("전세");
		}
		
		// 평수
		houseSizeTextView.setText(myData.getPung() + " 평");
		
		// 퍼센트
		BigDecimal percent = BigDecimal.ZERO;
		if (!bankMoney.equals("0"))
		{
			percent = bankMoney.divide(totalMoney, 2, BigDecimal.ROUND_UP);
			percent = percent.multiply(new BigDecimal("100"));
		}
		
		housePercentTextView.setText(percent + "%");
		
		// 지난날짜
		BigDecimal firstDate2 = new BigDecimal(MilkUtil.getDateDB(dates[0], dates[1], dates[2]));
		BigDecimal lastDate2 = new BigDecimal(MilkUtil.getDateDB(year, month+1, day));
		
		BigDecimal count2 = lastDate2.subtract(firstDate2);
		houseDayTextView.setText(count2 + " 일 째");
		
		// 집 모양 셋팅
		houseMessageTextView.setText("");
		if (percent.compareTo(new BigDecimal("20")) < 0)
		{
			houseImageView.setImageResource(R.drawable.ic_house_1);
		}
		else if (percent.compareTo(new BigDecimal("20")) > -1 && percent.compareTo(new BigDecimal("40")) < 0)
		{
			houseImageView.setImageResource(R.drawable.ic_house_2);
		}
		else if (percent.compareTo(new BigDecimal("40")) > -1 && percent.compareTo(new BigDecimal("60")) < 0)
		{
			houseImageView.setImageResource(R.drawable.ic_house_3);
		}
		else if (percent.compareTo(new BigDecimal("60")) > -1 && percent.compareTo(new BigDecimal("80")) < 0)
		{
			houseImageView.setImageResource(R.drawable.ic_house_4);
		}
		else if (percent.compareTo(new BigDecimal("80")) > -1)
		{
			houseImageView.setImageResource(R.drawable.ic_house_5);
		}
	}
	
	@Override
	protected void tipButton()
	{
	}
}
